package com.bjsxt.usersCenter.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
@Component("fileUtil")
public class FileUtil
{
	@Resource
	private RegexUtil regexUtil;
	public String readFile(File sourceFile)
	{
		try
		{
			
			return this.readFile(new FileInputStream(sourceFile));
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	public static String readFile(InputStream is)
	{
		StringBuffer sb=new StringBuffer();
		BufferedReader br=null;
		try
		{
			br=new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line="";
			while((line = br.readLine())!=null)
			{
				line=line.trim();
				if("".equalsIgnoreCase(line)||line.startsWith("//"))
				{
					continue;
				}
				sb.append(line+"\r\n");
			}
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(br!=null)
			{
				try
				{
					br.close();
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}
	public String replaceFile(String fileName,Map<String,String> paramsMap)
	{
		String resourceStr= this.readFile(ConstatFinalUtil.class.getClassLoader().getResourceAsStream(fileName));
		return this.regexUtil.replace$(resourceStr,paramsMap);
	}
	public static void main(String[] args)
	{
	
			String str="D:/项目空间/shoppping/usersCenter/usersCenter_common/src/main/resources/resourceInfo.json";
			FileUtil fileUtil=new FileUtil();
			File sourceFile=new File(str);
			String aa=fileUtil.readFile(sourceFile);
			System.out.println(aa);
	}	
	
}
